package com.jinlia.show.modules.base.utils;


import com.jinlia.show.modules.base.system.entity.SysAreaCode;
import com.jinlia.show.modules.base.system.service.AreaCodeMapperService;

import java.util.Map;
import java.util.stream.Collectors;

public class AdminCodeUtils {

    private static Map<Integer, String> cacheAdminCodeList;
    private static AreaCodeMapperService areaCodeMapperService;

    public static void newInstance(AreaCodeMapperService sysAdminCodeMapperService) {
        if (AdminCodeUtils.areaCodeMapperService == null) {
            AdminCodeUtils.areaCodeMapperService = sysAdminCodeMapperService;
        }
        cacheAdminCodeList = sysAdminCodeMapperService.lambdaQuery().list().stream().collect(Collectors.toMap(SysAreaCode::getCode, SysAreaCode::getCodeName));
    }

    public static String getProvinceName(Integer code) {
        code = code / 10000 * 10000;
        return cacheAdminCodeList.get(code);
    }

    public static String getCityName(Integer code) {
        code = code / 100 * 100;
        return cacheAdminCodeList.get(code);
    }

    public static String getAreaName(Integer code) {
        return cacheAdminCodeList.get(code);
    }

    public static String getCodeName(Integer code) {
        return cacheAdminCodeList.get(code);
    }
}
